package com.ds.infrastructure.hyperspace.container.admin.processor;

import com.ds.infrastructure.hyperspace.common.admin.AdminResponseVO;
import com.ds.infrastructure.hyperspace.common.config.OpentracingConfig;
import com.ds.infrastructure.hyperspace.container.internal.NettyContainer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * @Author: yaozh
 * @Description:
 */
@Slf4j
public class OpentracingConfigProcessor implements Processor {
    @Override
    public AdminResponseVO process(NettyContainer container, byte[] msg) throws IOException {
        log.info("Adjust opentracing config,input data:{}", StringUtils.toEncodedString(msg, UTF_8));
        OpentracingConfig opentracingConfig = NettyContainer.OBJECT_MAPPER.readValue(msg, OpentracingConfig.class);
        if (opentracingConfig == null) {
            String errorMsg = "Opentracing config invalid, config not found!";
            log.error(errorMsg);
            return AdminResponseVO.error(errorMsg);
        }
        if (StringUtils.isBlank(opentracingConfig.getUrl())) {
            opentracingConfig.setUrl(container.getOpentracingConfig().getUrl());
        }
        container.initOpentracing(opentracingConfig);
        return AdminResponseVO.OK;
    }
}
